package com.gdones.gpt.bus.chatdata.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gdones.common.core.dto.MyResult;
import com.gdones.gpt.bus.chatdata.dto.GptStaffChat;
import com.gdones.gpt.bus.chatdata.mapper.GptStaffChatMapper;
import com.gdones.gpt.bus.chatdata.service.IGptStaffChatService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author GaoJingBo
 * @since 2023-06-14
 */
@Service
public class GptStaffChatServiceImpl extends ServiceImpl<GptStaffChatMapper, GptStaffChat> implements IGptStaffChatService {

    @Autowired
    private GptStaffChatMapper staffChatMapper;

    @Override
    public MyResult getModelHisDataByStaffModesID(String staffModesID) {

        MyResult result = new MyResult();

        QueryWrapper<GptStaffChat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("staffModesID",staffModesID);

        List<GptStaffChat> gptStaffChats = staffChatMapper.selectList(queryWrapper);
        if(gptStaffChats.size()>0){
            result.setData(gptStaffChats.get(0));
            result.setMsg("历史记录查询成功！");
        }else{
            result.setData(new ArrayList<>());
            result.setMsg("历史记录查询成功-暂无记录！");
        }

        return result;
    }

    @Override
    public MyResult deleteModelHisDataByStaffModesID(String staffModesID) {

        MyResult result = new MyResult();

        int i = staffChatMapper.realDeleteByStaffModesID(staffModesID);

        result.setMsg("当前模型记录清除成功！");

        return result;
    }

    @Override
    public MyResult updateModelHisDataByStaffModesID(GptStaffChat dto) {

        MyResult result = new MyResult();

        // 先查询有没有数据
        QueryWrapper<GptStaffChat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("staffModesID",dto.getStaffModesID());

        List<GptStaffChat> gptStaffChats = staffChatMapper.selectList(queryWrapper);
        if(gptStaffChats.size()>0){ // 存在- 修改
            GptStaffChat gptStaffChat = gptStaffChats.get(0);
            gptStaffChat.setHisDatas(dto.getHisDatas());
            staffChatMapper.updateById(gptStaffChat);
            result.setMsg(null);
        }else{ // 添加
            int insert = staffChatMapper.insert(dto);
            result.setMsg(null);
        }

        return result;
    }
}
